package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.BaseMoveStoreMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseMoveStoreDetailEntity;
import com.kaili.basic.pojo.entity.BaseMoveStoreEntity;
import com.kaili.basic.pojo.vo.*;
import com.kaili.basic.service.BaseMoveStoreDetailService;
import com.kaili.basic.service.BaseMoveStoreService;
import com.kaili.basic.service.BaseStoreAccountsService;
import com.kaili.basic.service.BaseStoreStockService;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class BaseMoveStoreServiceImpl extends ServiceImpl<BaseMoveStoreMapper, BaseMoveStoreEntity> implements BaseMoveStoreService {

    @Autowired
    private BaseMoveStoreMapper baseMoveStoreMapper;

    @Autowired
    private BaseMoveStoreDetailService baseMoveStoreDetailService;

    @Autowired
    private BaseStoreStockService baseStoreStockService;

    @Autowired
    private BaseStoreAccountsService baseStoreAccountsService;

    @Override
    public Long queryStoreOrderNo() {
        return baseMoveStoreMapper.queryStoreOrderNo();
    }

    @Override
    public RestStatus addMoveStore(BaseMoveStoreAddDto addDto) {
        //插入移库记录
        BaseMoveStoreEntity entity=new BaseMoveStoreEntity();
        BeanUtils.copyProperties(addDto,entity);
        entity.setCreateTime(new Date());
        entity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());

        //统计数量
        for (BaseMoveStoreDetailAddDto detail:addDto.getDetailList()) {
            entity.setTotalTrays(entity.getTotalTrays()+detail.getTrays());
            entity.setTotalVo(entity.getTotalVo()+detail.getVolume());
            entity.setTotalWeight(entity.getTotalWeight()+detail.getWeight());
            entity.setAmount(entity.getAmount()+detail.getAmount());
        }
        baseMoveStoreMapper.insert(entity);

        //插入移库明细
        List<BaseMoveStoreDetailAddDto> list = addDto.getDetailList();
        baseMoveStoreDetailService.insertBatch(addDto.getDetailList(),addDto.getOrderNo());
        return CommonStatus.ADD_OK;
    }

    @Override
    public RestStatus updateMoveStore(BaseMoveStoreEditDto editDto) {
        //插入移库记录
        BaseMoveStoreEntity entity=new BaseMoveStoreEntity();
        BeanUtils.copyProperties(editDto,entity);
        entity.setModifyTime(new Date());
        entity.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        //统计数量
        for (BaseMoveStoreDetailAddDto detail:editDto.getDetailList()) {
            entity.setTotalTrays(entity.getTotalTrays()+detail.getTrays());
            entity.setTotalVo(entity.getTotalVo()+detail.getVolume());
            entity.setTotalWeight(entity.getTotalWeight()+detail.getWeight());
            entity.setAmount(entity.getAmount()+detail.getAmount());
        }
        //更新当前移库记录
        this.updateById(entity);
        //删除原有的产品明细
        baseMoveStoreDetailService.deleteByMap(new HashMap<String,Object>(){{put("order_no",editDto.getOrderNo());}});
        baseMoveStoreDetailService.insertBatch(editDto.getDetailList(),editDto.getOrderNo());
        return CommonStatus.UPDATE_OK;
    }

    @Override
    public RestStatus auditMoveStore(BaseMoveStoreAuditDto auditDto) throws RuntimeException {
        BaseMoveStoreEntity entity = this.selectById(auditDto.getId());
        //已经做过审核不做处理
        if(!("0".equals(entity.getIsAuduit()))){
            return CommonStatus.UPDATE_OK;
        }
        //1.更改移库单为已审核
        entity.setIsAuduit(auditDto.getIsAudit());
        entity.setAuditBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        entity.setAuditTime(new Date());
        this.updateById(entity);

        //审核通过做正式移库操作
        List<BaseMoveStoreDetailEntity> detailList= baseMoveStoreDetailService.selectByMap(new HashMap<String, Object>() {{
            put("order_no", auditDto.getOrderNo());
        }});

        //2.更新原库存 如果存在库存不足则抛出异常
        Boolean flage = baseStoreStockService.updateStock(detailList);
        if(!flage){
            throw new RuntimeException();
        }

        //3.批量新增新库存
        baseStoreStockService.bathInsert(detailList,this.selectById(entity.getId()).getSupplierId());

        //生成台账记录
        baseStoreAccountsService.insertBath(detailList);
        return CommonStatus.ADD_OK;
    }

    @Override
    public Page<BaseMoveStoreDetailVO> queryPageList(Page<BaseMoveStoreDetailVO> page, BaseMoveStoreQueryDto queryDto) {
        List<BaseMoveStoreDetailVO> list=baseMoveStoreMapper.queryPageList(page,queryDto);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<BaseMoveStoreDetailVO> queryDetailByOrderNo(HashMap<String, Object> map) {
        return baseMoveStoreMapper.queryDetailByOrderNo(map);
    }

    @Override
    public BaseMoveStoreDetailVO queryInStoreDetail(HashMap<String, Object> map) {
        BaseMoveStoreDetailVO moveStore= baseMoveStoreMapper.queryPageList(map);
        List<BaseMoveStoreDetailVO> list = baseMoveStoreMapper.queryDetailByOrderNo(new HashMap<String, Object>() {{
            put("orderNo", moveStore.getOrderNo());
        }});
        moveStore.setList(list);
        return moveStore;
    }

    @Override
    public List<BaseMoveStoreExportVO> export(BaseMoveStoreQueryDto queryDto) {
        return baseMoveStoreMapper.moveStoreExport(queryDto);
    }
}
